package YAPserver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import YAPenvironment.YAPconstant;

/**
 * The class which handles the high scores.
 * @author DMiagi
 *
 */
public class HighScore implements Comparable<HighScore>{

	private static List<HighScore> highScore= Collections.synchronizedList(new ArrayList<HighScore>());
	
	private int wins=0;
	private final String name;
	
	
	private HighScore(String name){
		this.wins=0;
		this.name=name;
	}
	
	private String getName(){
		return name;
	}
	private int getScore(){
		return wins;
	}
	private synchronized void increaseHighScore(){
		this.wins++;
	}
	
	/**
	 * Creates a high score for a player if new, otherwise doesn't do anything.
	 * @param name
	 */
	public static void createHighScore(String name){
		HighScore hS =getHighScore(name);
		if(hS!=null)
			return;
		//hS==null
		highScore.add(new HighScore(name));
	}
	
	/**
	 * Increases the high score of the specified client.
	 * @param name
	 */
	public static synchronized void increaseHighScore(String name){
		HighScore hS = getHighScore(name);
		if(hS!=null)
			hS.increaseHighScore();
	}
	
	/**
	 * Returns the high score for a name
	 * @param Name
	 * @return high score int numberOfWins
	 */
	static synchronized HighScore getHighScore(String Name){
		for(int i=0; i<highScore.size(); i++)
			if(highScore.get(i).getName().equals(Name))
				return highScore.get(i);
		return null;
	}
	/**
	 * @return the sorted high score in a whole string
	 */
	static synchronized String getHighScore(){
		Collections.sort(highScore);
		String string="";
		for(HighScore hS : highScore)
			string+=hS.getScore()+YAPconstant.C_SEPARATOR+hS.getName()+YAPconstant.C_NEWLINE;
		return string;
	}

	@Override
	public int compareTo(HighScore o) {
		if(wins < o.getScore())
			return 1;
		if(wins > o.getScore())
			return -1;
		return 0;
	}

}
